<template>
	<view class="player-manager">
		<view class="avatar-box">
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					头像上传
				</view>
				<view class="action">
					{{imgList.length}}/1
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					<image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length < 1">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			<view class="show-avatar">
				<view class="title">当前头像</view>
				<image :src="avatarImg"></image>
			</view>
			<view class="padding-xl">
				<button @tap="updateAvatar" class="cu-btn block bg-blue margin-tb-sm lg">更新头像</button>
			</view>
		</view>
		<view class="info-edit">
			<view class="cu-form-group">
				<view class="title">入学年份</view>
				<picker mode="date" :value="enrollmentYear" @change="DateChange">
					<view class="picker">
						{{enrollmentYear}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">性别</view>
				<picker @change="GenderPickerChange" :value="0" :range="gender">
					<view class="picker">
						{{genderIndex>-1?gender[genderIndex]:'请完善'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">QQ号</view>
				<input v-model="qq" placeholder="请完善QQ号" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">电话</view>
				<input v-model="phone" placeholder="请完善电话" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">地址</view>
				<input v-model="address" placeholder="请完善地址" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">身份证号</view>
				<input v-model="idCard" placeholder="请完善身份证号" name="input"></input>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">所在学校</view>
				<picker @change="SchoolPickerChange" :value="0" :range="school">
					<view class="picker">
						{{curSchoolIndex>-1?school[curSchoolIndex]:'请完善'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">所在学院</view>
				<picker @change="CollegePickerChange" :value="0" :range="college[curSchoolIndex]">
					<view class="picker">
						{{curSchoolIndex > 0 ? (curCollegeIndex > -1?college[curSchoolIndex][curCollegeIndex]:'请完善') : '请完善'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">专业</view>
				<input v-model="major" placeholder="请完善专业" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">宿舍地址</view>
				<input v-model="dormitoryAddr" placeholder="请完善宿舍地址" name="input"></input>
			</view>
			<view class="padding-xl">
				<button @tap="modifyBaseInfo()" class="cu-btn block bg-blue margin-tb-sm lg">保存上传</button>
			</view>
		</view>
		<view class="email-box">
			<view class="cu-form-group">
				<view class="title">邮箱</view>
				<input v-model="email" placeholder="请输入邮箱" name="input"></input>
				<button @tap="sendMailVerifyCode()" class="cu-btn block bg-green margin-tb-sm lg">发送验证码</button>
			</view>
			<view class="cu-form-group">
				<view class="title">邮箱验证码</view>
				<input v-model="emailVerifyCode" placeholder="请输入邮箱验证码" name="input"></input>
			</view>
			<view class="padding-xl">
				<button @tap="bindMailVerifyCode()" class="cu-btn block bg-blue margin-tb-sm lg">保存邮箱</button>
			</view>
		</view>
		<view class="password-box">
			<view class="cu-form-group">
				<view class="title">原密码</view>
				<input v-model="originPsd" placeholder="请输入原密码" name="input" password></input>
			</view>
			<view class="cu-form-group">
				<view class="title">新密码</view>
				<input v-model="newPsd" placeholder="请输入新密码" name="input" password></input>
			</view>
			<view class="cu-form-group">
				<view class="title">新密码</view>
				<input v-model="confirmNewPsd" placeholder="请确认新密码" name="input" password></input>
			</view>
			<view class="padding-xl">
				<button @tap="resetPlayerPassword()" class="cu-btn block bg-blue margin-tb-sm lg">修改密码</button>
			</view>
		</view>
	</view>
</template>

<script>
	import ajax from "../../../Ajax/index.js";
	import common from '../../../common/index.js'

	export default {
		name: "PlayerManager",
		data() {
			return {
				genderIndex: 0,
				gender: ['请完善', '男', '女'],
				school: [
					'请完善', '职大', '九院', '船校', '财校'
				],
				college: [
					[
						'请完善'
					],
					[
						'请完善', '师范学院', '学前教育学院', '艺术与体育学院', '机电工程学院', '建筑工程学院', '经济管理学院', '文化旅游学院', '信息工程学院', '护理学院', '农经学院', '中专部', '技师学院'
					],
					[
						'请完善', '马克思主义学院', '经济学院', '法学院', '教育学院', '体育学院', '文学与传媒学院外国语学院', '艺术学院', '理学院', '化学化工学院', '机械与智能制造学院', '材料科学与工程学院电子信息工程学院', '计算机与大数据科学学院', '建筑工程与规划学院', '医学院', '药学与生命科学学院', '护理学院', '管理学院', '旅游与地理学院', '电子商务学院', '国际交流学院', '继续教育学院', '资源环境学院'
					],
					[
						'请完善', '机械工程学院', '电气工程学院', '船舶工程学院', '汽车工程学院', '信息工程学院', '建筑工程学院', '财会金融学院', '经济管理学院', '继续教育学院'
					],
					[
						'请完善', '会计学院', '数字经济学院', '财税金融学院', '工商管理学院', '经济贸易学院', '信息工程学院', '外国语学院', '基础教育学院'
					]
				],
				selectedGender: '',
				selectedSchool: '',
				curSchoolIndex: 0,
				curCollegeIndex: 0,
				selectedCollege: '',
				enrollmentYear: '2000-10-10',
				major: '',
				dormitoryAddr: '',
				idCard: '',
				address: '',
				qq: '',
				phone: '',
				email: '',
				emailVerifyCode: '',
				originPsd: '',
				newPsd: '',
				confirmNewPsd: '',
				imgList: [],
				avatarImg: '../../../static/head.png'
			}
		},
		methods: {
			schoolChange(v) {
				let index = this.school.indexOf(v)
				this.curSchoolIndex = index
			},
			DateChange(e) {
				this.enrollmentYear = e.detail.value
			},
			GenderPickerChange(e) {
				this.genderIndex = e.detail.value
			},
			SchoolPickerChange(e) {
				this.curSchoolIndex = e.detail.value
			},
			CollegePickerChange(e) {
				this.curCollegeIndex = e.detail.value
			},
			urlBase64(url) {
			　　var toBase64Url;
			　　uni.request({
			　　　　url: url,
			　　　　method: 'GET',
			　　　　responseType: 'arraybuffer',
			　　　　success: async res => {
			　　　　　　let base64 = wx.arrayBufferToBase64(res.data); //把arraybuffer转成base64
			　　　　　　toBase64Url = 'data:image/jpeg;base64,' + base64; //不加上这串字符，在页面无法显示
			　　　　　　console.log(toBase64Url);
			　　　　}
			　　});
			},
			ChooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						console.log(res);
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '尊敬的玩家',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			updateAvatar() {
				if(this.imgList.length <= 0) {
					uni.showToast({title: '请上传头像图片', icon: 'none'})
					return
				}
				let playerName = common.getCookie('playerName')
				let playerId = common.getCookie('playerId')
				if (!!!playerName) {
					uni.showToast({title: '玩家名获取失败', icon: 'none'})
					return
				}
				if (!!!playerId) {
					uni.showToast({title: '玩家ID获取失败', icon: 'none'})
					return
				}
				let img = this.imgList[0]
				let that = this
				//限制图片大小
				uni.getImageInfo({
					src: img,
					success(res) {
						console.log(res);
						if(res.height > 1000 || res.width > 1000) {
							uni.showToast({
								title: '图片宽度或高度大于1000px 请自行裁剪',
								icon: 'none'
							})
							return
						}
						uni.request({
					　　　　url: img,
					　　　　method: 'GET',
					　　　　responseType: 'arraybuffer',
					　　　　success: async res => {
					　　　　　　 let base64 = wx.arrayBufferToBase64(res.data); //把arraybuffer转成base64
					　　　　　　 let toBase64Url = 'data:image/jpeg;base64,' + base64; //不加上这串字符，在页面无法显示
					　　　　　　 let param = new URLSearchParams()
								param.append("playerName", playerName)
								param.append("playerId", playerId)
								param.append("avatar", toBase64Url)
								ajax.post('/avatar/updateAvatar', param).then(res => {
									if (res.data.code === 200) {
										uni.showToast({title: res.data.msg, icon: 'none'})
										that.getAvatar()
									}
								})
					　　　　}
					　　});
					}
				})
			},
			getAvatar() {
				let param = new URLSearchParams()
				param.append("playerName", common.getCookie('playerName'))
				ajax.post('/avatar/getAvatar', param).then(res => {
					console.log(res);
					if (res.data.code === 200) {
						this.avatarImg = res.data.res.avatar
					}
				})
			},
			modifyBaseInfo() {
				let playerName = common.getCookie('playerName')
				if (!!!playerName) {
					uni.showToast({title: '玩家名获取失败', icon: 'none'})
					return
				}
				if (this.genderIndex === 0) {
					uni.showToast({title: '请输入性别', icon: 'none'})
					return
				}
				if (this.curSchoolIndex === 0) {
					uni.showToast({title: '请输入学校', icon: 'none'})
					return
				}
				if (this.curCollegeIndex === 0) {
					uni.showToast({title: '请输入学院', icon: 'none'})
					return
				}
				if (this.enrollmentYear === '2000-10-10') {
					uni.showToast({title: '请输入入学年份', icon: 'none'})
					return
				}
				if (this.major === '') {
					uni.showToast({title: '请输入专业', icon: 'none'})
					return
				}
				if (this.dormitoryAddr === '') {
					uni.showToast({title: '请输入宿舍地址', icon: 'none'})
					return
				}
				if (this.idCard === '' || !common.regex.idCard.test(this.idCard)) {
					uni.showToast({title: '请输入正确的身份证号', icon: 'none'})
					return
				}
				if (this.address === '') {
					uni.showToast({title: '请输入地址', icon: 'none'})
					return
				}
				if (this.qq === '' || !common.regex.qq.test(this.qq)) {
					uni.showToast({title: '请输入正确的QQ号', icon: 'none'})
					return
				}
				if (this.phone === '' || !common.regex.phone.test(this.phone)) {
					uni.showToast({title: '请输入正确的电话号码', icon: 'none'})
					return
				}
				let param = new URLSearchParams()
				param.append("playerName", playerName)
				param.append("gender", this.genderIndex == 1 ? '1' : '0')
				param.append("school", this.school[this.curSchoolIndex])
				param.append("college", this.college[this.curSchoolIndex][this.curCollegeIndex])
				param.append("enrollmentYear", this.enrollmentYear)
				param.append("major", this.major)
				param.append("dormitoryAddr", this.dormitoryAddr)
				param.append("idCard", this.idCard)
				param.append("address", this.address)
				param.append("qq", this.qq)
				param.append("phone", this.phone)
				ajax.post('/account/modifyBaseInfo', param).then(res => {
					console.log(res)
					if (res.data.code === 200) {
						uni.showToast({title: res.data.msg, icon: 'none'})
					} else {
						uni.showToast({title: res.data.msg, icon: 'none'})
					}
				})
			},
			sendMailVerifyCode() {
				if (this.email === '' || !common.regex.email.test(this.email)) {
					uni.showToast({title: '请输入正确的邮箱', icon: 'none'})
					return
				}
				let param = new URLSearchParams()
				param.append("mail", this.email)
				ajax.post('/account/sendBindMailVerifyCode', param).then(res => {
					if (res.data.code === 200) {
						uni.showToast({title: res.data.msg, icon: 'none'})
					} else {
						uni.showToast({title: res.data.msg, icon: 'none'})
					}
				})
			},
			bindMailVerifyCode() {
				let playerName = common.getCookie('playerName')
				if (!!!playerName) {
					console.log('玩家名获取失败')
					return
				}
				if (this.emailVerifyCode === '') {
					uni.showToast({title: '请输入邮箱验证码', icon: 'none'})
					return
				}
				if (this.email === '' || !common.regex.email.test(this.email)) {
					uni.showToast({title: '请输入正确的邮箱', icon: 'none'})
					return
				}
				let param = new URLSearchParams()
				param.append("playerName", playerName)
				param.append("mail", this.email)
				param.append("verifyCode", this.emailVerifyCode)
				ajax.post('/account/bindMail', param).then(res => {
					if (res.data.code === 200) {
						uni.showToast({title: res.data.msg, icon: 'none'})
					} else {
						uni.showToast({title: res.data.msg, icon: 'none'})
					}
					this.emailVerifyCode = ''
				})
			},
			resetPlayerPassword() {
				let playerName = common.getCookie('playerName')
				if (!!!playerName) {
					console.log('玩家名获取失败')
					return
				}
				if (this.originPsd === '' || this.originPsd.length < 6) {
					uni.showToast({title: '请输入大于6位原密码', icon: 'none'})
					return
				}
				if (this.newPsd === '' || this.newPsd.length < 6) {
					uni.showToast({title: '请输入大于6位新密码', icon: 'none'})
					return
				}
				if (this.newPsd === this.originPsd) {
					uni.showToast({title: '原密码与新密码相同', icon: 'none'})
					return
				}
				let param = new URLSearchParams()
				param.append("playerName", playerName)
				param.append("originPsd", this.originPsd)
				param.append("newPsd", this.newPsd)
				ajax.post('/account/resetPlayerPassword', param).then(res => {
					if (res.data.code === 200) {
						uni.showToast({title: res.data.msg, icon: 'none'})
					} else {
						uni.showToast({title: res.data.msg, icon: 'none'})
					}
					this.originPsd = ''
					this.newPsd = ''
					this.confirmNewPsd = ''
				})
			}
		},
		mounted() {
			let playerName = common.getCookie('playerName')
			if (!!!playerName) {
				console.log('玩家名获取失败')
				return
			}
			this.getAvatar()
			let param = new URLSearchParams()
			param.append("playerName", playerName)
			ajax.post('/account/getPlayerInfo', param).then(res => {
				console.log(res)
				if (res.data.code === 200) {
					this.genderIndex = res.data.playerInfo.gender == 1 ? 1 : 2
					console.log(this.genderIndex);
					this.school.map((v, i) => {
						if(v == res.data.playerInfo.school) {
							this.curSchoolIndex = i
						}
					})
					this.selectedCollege = res.data.playerInfo.college
					this.college[this.curSchoolIndex].map((v, i) => {
						if(v == res.data.playerInfo.college) {
							this.curCollegeIndex = i
						}
					})
					let date = new Date(res.data.playerInfo.enrollmentYear)
					this.enrollmentYear = date.getFullYear() + '-' + (date.getUTCMonth() + 1) + '-' + date.getDate()
					this.dormitoryAddr = res.data.playerInfo.dormitoryAddr
					this.idCard = res.data.playerInfo.idCard
					this.major = res.data.playerInfo.major
					this.address = res.data.playerInfo.address
					this.qq = res.data.playerInfo.qq
					this.phone = res.data.playerInfo.phone
					this.email = res.data.playerInfo.email
				}
			})
		}
	}
</script>

<style lang="scss" scoped>
	.player-manager {
		.avatar-box {
			.show-avatar {
				display: flex;
				justify-content: space-around;
				align-items: center;
				margin-top: 20rpx;
				image {
					width: 150rpx;
					height: 150rpx;
				}
			}
		}
		.info-edit {
			background-color: #FEFFFE;
		}
	}
</style>
